#pragma once


#include "FileReader.h"
#include <cstdlib>
#include <iostream>
#include <filesystem>
#include "DotFileWriter.h"
#include "memorymapping.h"
#include "xlnt/xlnt.hpp"



/**
 * @brief 去除字符串两端的空白字符
 * @param str 输入字符串
 * @return 去除空白后的字符串
 */
std::string trim(const std::string& str);
/**
 * @brief 对向量进行去重操作
 * @param vec 待去重的向量引用
 */
void deduplicate(std::vector<std::string>& vec);
/**
 * @brief 获取指定目录下特定扩展名的所有文件路径
 * @param dir_path 目录路径
 * @param extension 文件扩展名（如".docx"）
 * @return 包含文件路径的向量
 */
std::vector<std::string> get_files(const std::string& dir_path, const std::string& extension);

/**
 * @brief 获取目标需求的所有上游需求
 * @param retroRelationshipMap 追溯关系映射表
 * @param target 目标需求ID
 * @param flag 是否递归查找，默认为true
 * @return 包含所有上游需求ID的向量
 */
std::vector<std::string> getSourceRequirements(const std::unordered_map<std::string, std::vector<std::string>>& retroRelationshipMap, const std::string& target, bool flag = true);
/**
 * @brief 获取目标需求的所有下游需求
 * @param retroRelationshipMap 追溯关系映射表
 * @param target 目标需求ID
 * @param flag 是否递归查找，默认为true
 * @return 包含所有下游需求ID的向量
 */
std::vector<std::string> getDownstreamRequirements(const std::unordered_map<std::string, std::vector<std::string>>& retroRelationshipMap, const std::string& target, bool flag = true);

std::vector<std::string> WriteToDotFile(
    const std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& contentMap, 
    const std::unordered_map<std::string, std::vector<std::string>>& retroRelationshipMap,
    const std::string& target);

// Function to run the main logic of reading DOCX files from a directory
void run(const std::string& dirPath, const std::string& target, const bool& accept_flag, const bool& main_flag, const bool& search_flag = false);

void transferToPng(const std::string& path, const std::string& target);
void removeDotFile(const std::string& target);
void writeToExcel(
    const std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& contentMap, 
    std::unordered_map<std::string, std::vector<std::string>> retroRelationshipMap,
    const std::vector<std::string>& target_list,
    const std::string& path,
    const std::string& target);

void setupExcelSheet(xlnt::worksheet* const worksheet, const std::string& sheetName);

void writeCompleteness(
    xlnt::worksheet* worksheet, 
    const std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& contentMap, 
    std::unordered_map<std::string, std::vector<std::string>> retroRelationshipMap,
    const std::vector<std::string>& target_list);

void writeTraceability(
    xlnt::worksheet*  worksheet, 
    const std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& contentMap, 
    std::unordered_map<std::string, std::vector<std::string>> retroRelationshipMap,
    const std::vector<std::string>& target_list);

void changeDocxAttributes(const std::string& path);

void writeContentMapToSharedMem(
    MemoryShared::MemoryMapping& mem,
    std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& data);

std::unordered_map<std::string, std::unordered_map<std::string, std::string>> readContentMapFromSharedMem(MemoryShared::MemoryMapping& mem);

void writeRetroMapToSharedMem(
    MemoryShared::MemoryMapping& mem,
    std::unordered_map<std::string, std::vector<std::string>>& data);

std::unordered_map<std::string, std::vector<std::string>> readRetroMapFromSharedMem(MemoryShared::MemoryMapping& mem);

std::vector<std::string> getAllRequirementsList(
    const std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& contentMap, 
    std::unordered_map<std::string, std::vector<std::string>> retroRelationshipMap,
    const std::string& target);

void createVerificationSheets(
    const std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& contentMap, 
    std::unordered_map<std::string, std::vector<std::string>> retroRelationshipMap,
    const std::vector<std::string>& target_list,
    const std::string& path);

bool compareSafetyLevels(const std::string& level1, const std::string& level2);
void readConfig(const std::string& filePath);

namespace MyXlnt {

class AbstractDecorator : public xlnt::worksheet
{
public:
    // 构造函数，接受一个 xlnt::worksheet 指针
    AbstractDecorator(xlnt::worksheet* ws) : m_ws(ws) {}
    virtual ~AbstractDecorator() = default;

    // 纯虚函数，子类必须实现
    virtual void writeWithWrap(int col, int row, const std::string& value) = 0;
protected:
    xlnt::worksheet* m_ws;
};



class Decorator_Worksheet : public AbstractDecorator
{
public:
    Decorator_Worksheet(xlnt::worksheet* ws) : AbstractDecorator(ws) {}
    ~Decorator_Worksheet() = default;

    void writeWithWrap(int col, int row, const std::string& value) {
        xlnt::alignment alignment;
        alignment.wrap(true); // Enable text wrapping
        alignment.horizontal(xlnt::horizontal_alignment::left); // 设置单元格内容水平居中
        m_ws->cell(col, row).alignment(alignment);
        m_ws->cell(col, row).value(value);
    }

};


}
